import java.util.HashMap;

public class demo1 {
    //LCR 017. 最小覆盖子串
    public String minWindow(String s, String t) {
        int len=Integer.MAX_VALUE;
        int left=0,right=0;
        HashMap<Character,Integer>hashMap1=new HashMap<>();
        HashMap<Character,Integer>hashMap2=new HashMap<>();
        for(int i=0;i<t.length();i++){
            hashMap2.put(t.charAt(i),hashMap2.getOrDefault(t.charAt(i),0)+1);
        }
        int n=s.length();
        String ret="";
        int count=0;
        while (left<=right&&right<n){
            hashMap1.put(s.charAt(right),hashMap1.getOrDefault(s.charAt(right),0)+1);
            if(hashMap1.get(right)==hashMap2.get(right))count++;
            if(count==hashMap2.size()){
                while (left<=right){
                    hashMap1.remove(left);
                    if(hashMap2.containsKey(s.charAt(left))){
                        if (hashMap1.getOrDefault(hashMap1.get(s.charAt(left)), 0) + 1 != hashMap1.get(s.charAt(left))) {
                            count--;
                            break;
                        }
                    }
                    if(right-left+1<len){
                        len=right-left+1;
                        ret=s.substring(left,right);
                    }
                    left++;
                }
            }
        }
        return ret;
    }
}
